package chenjun.mydatabindingdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import chenjun.mydatabindingdemo.R;
import chenjun.mydatabindingdemo.customview.LargeImageView;
import chenjun.mydatabindingdemo.util.Logger;

public class ImageActivity extends BaseActivity {

    private String path;
    private LargeImageView iv;
    private InputStream is;
    private ByteArrayOutputStream baos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initIntent();
        loadBitmap();
    }

    @Override
    protected void setView() {
        setContentView(R.layout.activity_image);
    }

    @Override
    protected void setListener() {

    }

    @Override
    protected void initViews() {
        iv = (LargeImageView) findViewById(R.id.iv);
    }

    @Override
    protected void initData() {

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            iv.setInputStream(baos.toByteArray());
        }
    };

    private void loadBitmap() {
        Logger.i(path + "");
        new Thread(new Runnable() {

            @Override
            public void run() {
                is = null;
                HttpURLConnection conn = null;
                try {
                    conn = (HttpURLConnection) new URL(path).openConnection();
                    is = conn.getInputStream();
                    int code = conn.getResponseCode();
                    if (code == HttpURLConnection.HTTP_OK) {
                        baos = new ByteArrayOutputStream();
                        byte[] buf = new byte[2048];
                        int len = -1;
                        while ((len = is.read(buf)) != -1) {
                            baos.write(buf, 0, len);
                        }
                        baos.flush();
                        handler.sendEmptyMessage(0);
                    }
                } catch (IOException e) {
                    Logger.i("loadBitmap--IOException");
                    e.printStackTrace();
                } finally {
                    if (conn != null) {
                        conn.disconnect();
                        conn = null;
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        is = null;
                    }
                }
            }
        }).start();
    }

    private void initIntent() {
        if (getIntent() != null) {
            Intent intent = getIntent();
            path = intent.getStringExtra("path");
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (baos != null) {
            try {
                baos.close();
                baos = null;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
